package yanivosher.aiproject;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Scanner;

public class InputParser {

	public static void parseFile(String path)
	{
		FileInputStream fis = null;
		InputStreamReader in = null;
		try {
			fis = new FileInputStream(path);
			in = new InputStreamReader(fis, "ASCII");
			
			Scanner sc = new Scanner(in);
			while (sc.hasNextLine()) {
				String line = sc.nextLine();
				if (line.length() > 3) {
					switch (line.substring(0, 3)) {
					case "#V ":
						parseVertex(line.substring(2,line.indexOf(";")));
						break;
					case "#E ":
						parseEdge(line.substring(2,line.indexOf(";")));
						break;
					case "#C ":
						parseChems(line.substring(2,line.indexOf(";")));
						break;
					case "#M ":
						parseMilitry(line.substring(2,line.indexOf(";")));
						break;
	
					default:
						break;
					}
				}
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (in != null) {
				try {
					in.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if (fis != null) {
				try {
					fis.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		
	}
	
	private static void parseVertex(String line) {
		int vertexNum = Integer.parseInt(line.trim());
		if (vertexNum > 0)
		{
			for (int i = 0; i < vertexNum; i++) {
				Environment.getInstance().graph.getVertexes().add(new Vertex(String.valueOf(i), String.valueOf(i)));
			}
		}
	}
	
	private static void parseEdge(String line) {

		String[] str = line.trim().split(" ");
		Environment.getInstance().graph.getEdges().add(new Edge(str[0] + str[1]
																, Environment.getInstance().graph.getVertexById(str[0])
																, Environment.getInstance().graph.getVertexById(str[1])
																, Integer.parseInt(str[2].substring(1))
																, str[3].equals("B") ? true : false));
		Environment.getInstance().graph.getEdges().add(new Edge(str[1] + str[0]
																, Environment.getInstance().graph.getVertexById(str[1])
																, Environment.getInstance().graph.getVertexById(str[0])
																, Integer.parseInt(str[2].substring(1))
																, str[3].equals("B") ? true : false));
	}
	
	private static void parseChems(String line) {

		System.out.println("Parsing chems: " + line);
		String[] str = line.trim().split(" ");
		Vertex v = Environment.getInstance().graph.getVertexById(str[0]);
		System.out.println("Parsing chems - Vertex: " + v);
		v.setChem(v.getChem() + Integer.parseInt(str[1]));
	}

	private static void parseMilitry(String line) {

		String[] str = line.trim().split(" ");
		Vertex v = Environment.getInstance().graph.getVertexById(str[0]);
		v.setMilitary(v.getMilitary() + Integer.parseInt(str[1]));
	}
}
